<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"   lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>身份管理</title>
        <link rel="stylesheet" type="text/css" href="/js/jquery-easyui-1.5.4.5/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="/js/jquery-easyui-1.5.4.5/themes/icon.css">
        <script src="/js/jquery-easyui-1.5.4.5/jquery.min.js"></script>
        <script src="/js/jquery-easyui-1.5.4.5/jquery.easyui.min.js"></script>
    </head>

    <body>
	    <table class="easyui-datagrid" title="身份管理" style="width:90%;height:500px" id="table-grid"
			data-options="singleSelect:true,collapsible:false,url:'/baseControl/GET/cn.com.fig.dao.IdentityMapper.list',method:'get',toolbar:toolbar,pagination:true,pageSize:20">
		<thead>
			<tr>
				<th data-options="field:'identityId',width:200">身份编号</th>
				<th data-options="field:'identityName',width:400">身份名称</th>
				<th data-options="field:'departName',width:400">所属部门</th>
				<th data-options="field:'groupName',width:400">岗位名称</th>
				<th data-options="field:'mainPage',width:400">首页地址</th>
				<th data-options="field:'identityStatus',width:100,formatter: function(value,row,index){
				if(value == '0') return '无效';
				if(value == '1') return '有效';
			}">岗位状态</th>
			</tr>
		</thead>
	</table>
	<div id="saveWindows" class="easyui-window" title="Modal Window" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:500px;height:300px;padding:10px;">
		<form id="ff" class="easyui-form" method="post" data-options="novalidate:true">
			<table>
				<tr>
					<td width="80px">身份编号:</td>
	    			<td>
	    				<input class="easyui-textbox" type="text" name="identityId" id="identityId" data-options="required:true" width='300px'></input>
	    			</td>
				</tr>
				<tr>
					<td>身份名称:</td>
	    			<td>
	    				<input class="easyui-textbox" type="text" name="identityName" id="identityName" data-options="required:true" width="300px"></input>
	    			</td>
				</tr>
				<tr>
					<td>所属部门:</td>
	    			<td>		    			
						<select id="departId" class="easyui-combobox" name="departId"  style="width:200px;" 
						data-options="
							valueField: 'departId',
						    textField: 'departName',
						    url: '/dictionary/list?dicId=depart',
						    required:true
						">
						</select>
					</td>
				</tr>
				<tr>
					<td>岗位名称:</td>
	    			<td>		    			
						<select id="groupId" class="easyui-combobox" name="groupId"  style="width:200px;" 
						data-options="
							valueField: 'groupId',
						    textField: 'groupName',
						    url: '/dictionary/list?dicId=group',
						    required:true
						">
						</select>
					</td>
				</tr>
				<tr>
					<td>首页地址:</td>
	    			<td>
	    				<input class="easyui-textbox" type="text" name="mainPage" id="mainPage" data-options="required:true" width="300px"></input>
	    			</td>
				</tr>
				<tr>
					<td>身份状态:</td>
	    			<td>		    			
						<select id="identityStatus" class="easyui-combobox" name="identityStatus"  style="width:200px;">
						    <option value="1">有效</option>
						    <option value="0">无效</option>
						</select>
						<input id='insertMark' name='insertMark' type='hidden' value='0'>
					</td>
				</tr>
				<tr>
				<td><input type='button' onclick="submitForm();" value='保存'/></td>
				<td><input type='button' onclick="$('#saveWindows').window('close');" value='取消'/></td>
				</tr>
			</table>
		</form>
	</div>
	<!-- 身份拥有菜单页面元素操作权限 -->
	<div id="elementWindows" class="easyui-window" title="Modal Window" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:850px;height:500px;padding:10px;overflow: hidden;">
		 <div id="cc" class="easyui-layout" style="width:800px;height:500px;">
			 <div data-options="region:'west',title:'页面功能',split:true,collapsible:false" style="width:300px;">
			 	<table class="easyui-datagrid"  style="width:98%;height:300px" id="e-grid"
						data-options="singleSelect:false,collapsible:false,method:'get',pagination:false,idField:'elementId',toolbar:'#elementToolbar'">
					<thead>
						<tr>				
							<th data-options="field:'elementId',width:100,checkbox:true"></th>
							<th data-options="field:'elementName',width:200">元素名称</th>
						</tr>
					</thead>
				</table>
			 </div>
			  <div data-options="region:'center',split:true" style="width:100px;text-align:center;padding-top:150px; ">
			 <input type="image" src="/images/navigate-right.png" width="50px" height="50px" onclick="elementRightMove();">
			 <br><br><br>
			 <input type="image" src="/images/navigate-left.png" width="50px" height="50px" onclick="elementLeftMove();">
			  </div>
	    	 <div data-options="region:'east',title:'身份拥有的页面功能',split:true,collapsible:false" style="width:400px;">
					 <table class="easyui-datagrid"  style="width:98%;height:450px" id="element-grid"
							data-options="singleSelect:true,collapsible:false,method:'get',pagination:false,idField:'idementID'">
						<thead>
							<tr>				
								<th data-options="field:'idementID',width:50,checkbox:true"></th>
								<th data-options="field:'privilName',width:100">菜单名称</th>
								<th data-options="field:'elementId',width:150">页面元素编号</th>
								<th data-options="field:'elementName',width:200">元素名称</th>
							</tr>
						</thead>
					</table>
			</div>
	</div>
	</div>
	<!-- 菜单信息相关操作 -->
	<div id="privilegeWindows" class="easyui-window" title="Modal Window" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:800px;height:500px;padding:10px;overflow: hidden;">
		 <table class="easyui-treegrid" title="角色菜单管理" style="width:98%;height:500px" id="privilege-grid"
			data-options="idField:'privilegeId',treeField:'privilName',singleSelect:false,collapsible:false,method:'get',pagination:false">
		<thead>
			<tr>				
				<th data-options="field:'privilegeId',width:60,checkbox:true"></th>
				<th data-options="field:'privilName',width:400">菜单名称</th>
				<th data-options="field:'sysName',width:100">所属系统</th>
				<th data-options="field:'identityName',width:100">所属身份</th>
			</tr>
		</thead>
	</table>
	</div>
	<div id="elementToolbar" style="padding:5px;height:auto">
	菜单: 
			<select id="privilegeTree" class="easyui-combotree" data-options="editable:true" style="width:240px">
			</select>
	</div>
  <script type="text/javascript">		
	//dataGrid工具栏
	var toolbar = [{
				text:'新增',
				iconCls:'icon-add',
				handler:function(){
					var tt = $('#table-grid');
					var node = tt.datagrid('getSelected');
					var pId = null;
					$('#insertMark').val('1');
					$("#saveWindows").window('open');
				}
			},{
				text:'编辑',
				iconCls:'icon-edit',
				handler:function(){
					var tt = $('#table-grid');
					var row=tt.datagrid('getSelected');
					if(row){
						$("#saveWindows").window('open');
						$('#insertMark').val('0');
						$('#identityId').textbox("setValue",row.identityId);
						$('#identityName').textbox("setValue",row.identityName);
						$('#mainPage').textbox("setValue",row.mainPage);
						$('#identityStatus').combobox("select",row.identityStatus);
						$('#groupId').combobox("select",row.groupId);
						$('#departId').combobox("select",row.departId);
						
					}else{
						$.messager.alert('Warning','请选择要编辑的行！');
					}
				}
			},{
				text:'删除',
				iconCls:'icon-remove',
				handler:function(){
							var tt = $('#table-grid');
							var row=tt.datagrid('getSelected');
							if(!row){
								$.messager.alert('Warning','请选择要删除的行！');
							}
					$.messager.confirm('提示','您确定要删除吗？',function (r){
						if(r){
							$.ajax({
								   type: "POST",
								   dataType:"json",
								   url: "/baseControl/DELETE/cn.com.fig.dao.IdentityMapper.delete",
								   data: {priId:row.identityId},
								   success: function(msg){
								     if(msg){
								    	 $.messager.alert('Warning',msg.retMsg);
								    	 $('#table-grid').datagrid('reload');
								     }
								   }
								});
						}
					});
				}
			},'-',{
				text:'菜单维护',
				iconCls:'icon-large-smartart',
				handler:function(){
					var tt = $('#table-grid');
					var row=tt.treegrid('getSelected');
					if(row == null){
						$.messager.alert('Warning','请选择要操作的行！');
					}

					$("#privilege-grid").treegrid({
						url:'/baseControl/GET/cn.com.fig.dao.IdenpriMapper.list',
						queryParams:{
							identityId:row.identityId,
							needSplitPage:false
						},
			 			onLoadSuccess:function(row, data){
							cWindow = false;
							var rr = data.rows;
							$("#privilege-grid").treegrid("unselectAll");
							for(var i=0;i<rr.length;i++){
								if(rr[i].idenPriId){
									$("#privilege-grid").treegrid("select",rr[i].privilegeId);
								}
							}
							cWindow = true;
						}, 
						onCheck:function(rowData){
							if(!cWindow) 
								return;
							
							$.ajax({
								   type: "POST",
								   dataType:"text",
								   url: "/uuidGen/genUUID",
								   success: function(msg){
								     if(msg){
								    	 $.ajax({
											   type: "POST",
											   dataType:"json",
											   url: "/baseControl/PUT/cn.com.fig.dao.IdenpriMapper.insert",
											   data:{idenPriId:msg,identityId:row.identityId,privilegeId:rowData.privilegeId,insertMark:'1'},
											   success: function(msg){
												   if(msg && msg.retCode == '00'){
												    	 $.messager.show({
												    		 title:"提示",
												    		 msg:'成功添加身份和菜单关系',
												    		 timeout:500,
												    		 showType:'slide'
												    	 });
												    	 $("#privilege-grid").treegrid("reload");
												     }
											   }
											});											 
								     }
								   }
								});
						},
						onUncheck:function(rowData){
							if(!cWindow) 
								return;
							if(rowData.privilegeId){
									$.ajax({
										   type: "POST",
										   dataType:"json",
										   url: "/baseControl/DELETE/cn.com.fig.dao.IdenpriMapper.delete",
										   data: {priId:rowData.idenPriId},
										   success: function(msg){
										     if(msg && msg.retCode == '00'){
										    	 $.messager.show({
										    		 title:"提示",
										    		 msg:'成功解除身份和菜单关系',
										    		 timeout:500,
										    		 showType:'slide'
										    	 });
										     }
										   }
										});
							}
						}
					});
					
					$("#privilegeWindows").window('open');
				}
			},{
				text:'页面操作权限维护',
				iconCls:'icon-large-smartart',
				handler:function(){
					var tt = $('#table-grid');
					var row=tt.treegrid('getSelected');
					if(row == null){
						$.messager.alert('Warning','请选择要操作的行！');
					}

					$("#element-grid").datagrid({
						url:'/baseControl/GET/cn.com.fig.dao.IdementMapper.list',
						queryParams:{
							identityId:row.identityId,
							needSplitPage:false
						}
					});
					$('#privilegeTree').combotree({
					    url: '/privilege/privilegeTree',
					    queryParams:{level:0},
					    onClick: function(node){
					    	var tt = $('#privilegeTree').combotree("tree");
					    	var isLeaf = tt.tree("isLeaf",node.target);  
					    	if(isLeaf){
					    		$("#e-grid").datagrid("clearSelections");
					    		$("#e-grid").datagrid({
									url:'/baseControl/GET/cn.com.fig.dao.ElementMapper.list',
									queryParams:{
										privilegeId:node.id,
										needSplitPage:false
									}
								});
					    	}
						},
						onBeforeLoad: function(node, param){
							if(node){
								var tt = $('#privilegeTree').combotree("tree");
						    	var isLeaf = tt.tree("isLeaf",node.target);
								var systemId = node.attributes.systemId;
								var privillegeId = node.attributes.privillegeId;
								var leaf =  node.attributes.leaf;
								if(systemId){
									param.level = 1;
								}else if(privillegeId){
									param.level = 2;
								}else if(leaf){
									param.level = 3;
								}
							}
						}
					});
					$("#elementWindows").window('open');
				}
			}];
	
	//页面操作功能权限维护
	function elementRightMove(){
			var row= $("#table-grid").datagrid('getSelected');
			
			var tt = $('#e-grid');
			var node = tt.datagrid('getSelections');
		 	var idements = new Array();
			for(var i=0;i<node.length;i++){
				var dd = new Object();
				dd.identityId = row.identityId;
				dd.elementId = node[i].elementId;
				idements[i] = dd;
			} 
			var param = JSON.stringify(idements);
			$.ajax({
				   type: "POST",
				   dataType:"json",
				   url: "/baseControl/BATCHPUT/cn.com.fig.dao.IdementMapper.insertBatch",
				   data: {
					   param:param,
					   keyGen:"UUID",
					   primary:"idementID"
					   },
				   success: function(msg){
				     if(msg){
				    	 $.messager.alert('Warning',msg.retMsg);
				    	 $('#element-grid').datagrid('reload');
				     }
				   }
				});
		}
	function elementLeftMove(){
		var row= $("#element-grid").datagrid('getSelected');
		$.ajax({
			   type: "POST",
			   dataType:"json",
			   url: "/baseControl/DELETE/cn.com.fig.dao.IdementMapper.delete",
			   data: {
				   priId:row.idementID
				   },
			   success: function(msg){
			     if(msg){
			    	 $.messager.alert('Warning',msg.retMsg);
			    	 $('#element-grid').datagrid('reload');
			     }
			   }
			});
	}
	$(function(){
		$('#ff').form({
			url:"/baseControl/POST",
			onSubmit: function(){
				var isValid = $(this).form('validate');
				if (!isValid){
					$.messager.alert('Warning','请填写必填项');
				}
				return isValid;	
			},
			success:function(msg){
			     if(msg){
			    	 var data = eval('(' + msg + ')');
			    	 $.messager.alert('Warning',data.retMsg);
			    	 $('#table-grid').datagrid('reload');
			    	 $("#saveWindows").window('close');
			     }
			}
		});
		var pager = $('#table-roleGrid').datagrid().datagrid('getPager');
		pager.pagination({
			displayMsg: ''
		});
	});
	
	function submitForm(){
		var insertMark = $('#insertMark').val();
		var reqUrl = ''
		if(insertMark == 0){
			reqUrl = '/baseControl/POST/cn.com.fig.dao.IdentityMapper.update';
		}else{
			reqUrl = '/baseControl/PUT/cn.com.fig.dao.IdentityMapper.insert';
		}
		
		$('#ff').form("submit",{
			url:reqUrl
		});	
	}
	</script>      
    </body>
</html>
